{extend name="../admin/common/base" /}

{block name="body"}
<div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
    <div style="padding: 0px 10px 0px 10px">
        <div class="layui-tab">
            <ul class="layui-tab-title">
                <a href="{:url('groupSend')}"><li  class="layui-this">群发消息</li></a>
                <a href="{:url('sendRecord')}"><li>发送记录</li></a>

            </ul>
        </div>

    </div>

    <div class="layui-tab-content">

        <form id="select-wrap" class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <div class="layui-form-item">
                        <label class="layui-form-label">公众号</label>
                        <div class="layui-input-block ">
                            <div class="select-allmp">
                                <input type="checkbox"  title="全选" lay-skin="primary" lay-filter="allChoose" id="selectAllMp">
                            </div>

                            {volist name="mpList" id="mp"}
                            <div class="select-mplist">
                                <div class="logo-left">
                                    <img src="{$mp.logo}">
                                </div>
                                <div class="logo-right" style="width: 112px;overflow: hidden;">
                                    <p class="p1">{$mp.name}</p>
                                    <p class="p2">类型：{$mp.type_name}</p>
                                </div>
                                <input type="checkbox" name="select-mp" class="select-mp" value="{$mp.id}"  lay-ignore>
                            </div>
                            {/volist}

                        </div>
                    </div>


                    <div class="layui-form-item">
                        <label class="layui-form-label">素材</label>
                        <div class="layui-input-block ">
                            <div class="add-material" id="select-material" onclick="selectMaterial()">
                                选择素材

                            </div>
                            <a class="add-material" href="{:url('addMaterial')}" target="_blank">
                                新增素材

                            </a>

                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <input type="button" class="layui-btn layui-btn-normal" id="getAllFriend"  value="同步粉丝信息">
                            <input type="button" class="layui-btn" onclick="sendMessage()" value="立即群发">
                        </div>
                    </div>

                </div>

            </div>

        </form>
    </div>



</div>

<script>
    layui.use(['layer'],function () {
        var layer=layui.layer;
    });

    function selectMaterial(){layer.open({type: 2,title: '选择素材',shadeClose: true,shade: 0.1,area: ['820px', '520px'],
        content: '/admin/material/getSendMaterial'
    })}
    var news_id = 0;
    var news_type = '';
    var news_from_type = '';

    function controllerByVal1(value,type,from_type) {

        if(value > 0) {
            $.post("{:url('getMaterialInfo')}",
                {
                    'id': value,
                    'type': type,
                    'from_type':from_type
                },
                function (res) {
                    if (res.status == 1) {
                        var html = '';
                        if(type == 'image'){
                            html = "<img src='"+res.url+"'  alt=''/>";
                            $('#select-material').html(html);
                        }else if(type == 'news'|| type == 'activity'){
                            html = "<div class='material-content'>" +
                                "<img src='"+res.url+"'  alt=''/>"+
                                "<p>"+res.title+"</p>" +
                                "</div>";
                            $('#select-material').html(html);


                        }else if(type == 'text'){
                            html = "<div style='line-height: 25px;margin: 10px;text-align: left;height: 180px;overflow: hidden;background: #ffffff'>" +
                                res.content+
                                "</div>";
                            $('#select-material').html(html);
                        }
                        news_id = value;
                        news_type = type;
                        news_from_type = from_type;
                    }else{
                        layer.alert(res.msg);
                    }
                });
        }else{
            layer.alert('素材不存在，请重新选择');
        }


    }
    
    function sendMessage() {

        layui.use('layer', function(){
            var layer = layui.layer;
            layer.confirm('你确定需要群发吗？', {
                btn: ['是','不'] //按钮
            }, function(){
                var selected_Mps = $("input[name='select-mp']:checked");

                var  selected_ids = '';
                if(selected_Mps.length > 0){
                    var selected_id_arr = [];
                    selected_Mps.each(function () {
                        selected_id_arr.push($(this).val());
                    });
                    selected_ids = selected_id_arr.join(',');

                }else{
                    layer.alert('请选择公众号');
                    return;
                }

                if(news_id > 0){
                    var loadIndex = layer.load(1);
                    $.post("{:url('admin/Material/sendMessage')}",{'id':news_id,'mp_ids':selected_ids,'type':news_type,'from_type':news_from_type},function (res) {
                        if(res.status==1){
                            layer.alert(res.msg);
                        }else{
                            layer.alert(res.msg);
                        }
                        layer.close(loadIndex);
                    })
                }else{
                    layer.alert('请选择素材');

                }


            }, function(){

            });
        });

    }
    layui.use('layer', function() {
        var $ = layui.jquery, layer = layui.layer;
        var lastOpenid = '';
        var page = '1';
        $('#getAllFriend').click(function () {
            //send(lastOpenid,page);

            var openids=[];
            $("input[name='select-mp']:checked").each(function (key,value) {
                openids[key]=$(this).val();
            });
            var openid_str = openids.join(',');

            if(openid_str.length > 0){
                var api_url = "{:url('mp/Index/SynSelectMp',['mids'=>'openid_strs'])}";
                api_url = api_url.replace(/openid_strs/, openid_str);
                layer.open({
                    type: 2,
                    title: '同步全部粉丝',
                    shadeClose: true,
                    shade: 0.5,
                    area: ['680px', '200px'],
                    content:api_url,
                    cancel: function(index, layero){

                    }
                });

            }else{
                layer.msg('请选择公众号');
            }

        });

    });
    layui.use('form', function () {
        var form = layui.form;
        form.on('checkbox(allChoose)', function(data){

            if($(data.elem).prop("checked")) {

                $('.select-mplist').addClass('on');
                $('.select-mp').prop('checked', true);

            }else{
                $('.select-mplist').removeClass('on');
                $('.select-mp').prop('checked',false);
            }
            form.render('checkbox');
        });
        $(function () {
            $('.select-mp').change(function () {
                if($(this).prop("checked")){
                    $(this).parent('.select-mplist').addClass('on');
                }else{
                    $(this).parent('.select-mplist').removeClass('on');
                }
            });

        })
    });

  



</script>
{/block}
